<?php

namespace App\Http\Middleware;

use App\Exceptions\ApiException;
use App\Models\LogAccess;
use Closure;

class GetIp
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->expectsJson()) {
            throw new ApiException('data not find', 404);
        }

        $ip = getIp();

        LogAccess::create([
            'ip' => $ip,
        ]);

        return $next($request);
    }
}
